/*
 * @Author: i@rysa.cn
 * @Date: 2022-05-14 17:06:39
 * @LastEditTime: 2022-07-26 09:44:52
 * @LastEditors: i@rysa.cn
 * @Description:
 * @FilePath: \go-utils\wechat\pay\refund.go
 */
package pay

import (
	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
)

type refund struct {
	*PayConfig
}

func NewRefund(cfg *PayConfig) *refund {
	return &refund{cfg}
}

func (p *refund) Refund(ref RefundRequest) (*refunddomestic.Refund, *core.APIResult, error) {
	client, ctx, err := GetClient(p.PayConfig)
	if err != nil {
		return nil, nil, err
	}
	svc := refunddomestic.RefundsApiService{Client: client}
	resp, result, err := svc.Create(ctx, refunddomestic.CreateRequest{
		SubMchid:      core.String(ref.SubMchid),
		TransactionId: core.String(ref.TransactionId),
		OutTradeNo:    core.String(ref.OutTradeNo),
		OutRefundNo:   core.String(ref.OutRefundNo),
		Reason:        core.String(ref.Reason),
		NotifyUrl:     core.String(ref.NotifyUrl),
		Amount: &refunddomestic.AmountReq{
			Total: core.Int64(int64(ref.Amount * 100)),
		},
	})
	return resp, result, err
}
